package com.palm.hubo2o.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.palm.hubo2o.BaseFragment;
import com.palm.hubo2o.R;
import com.palm.hubo2o.model.UserCash;
import com.palm.hubo2o.network.PalmCallBack;
import com.palm.hubo2o.network.PalmRetrofit;
import com.palm.hubo2o.network.PalmSession;
import com.palm.hubo2o.network.Request;
import com.palm.hubo2o.ui.LoginActivity;
import com.palm.hubo2o.ui.ModifyPwdActivity;
import com.palm.hubo2o.ui.UserInfoActivity;
import com.palm.hubo2o.ui.WithdrawActivity;
import com.palm.palmutils.DrawableUtils;
import com.palm.palmutils.widget.CellView;
import com.palm.palmutils.widget.CircleImageView;

import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by dest1 on 2015/10/20.
 */
public class CenterFragment extends BaseFragment {
    @Bind(R.id.tv_recharge_center)
    TextView tvRecharge;
    @Bind(R.id.tv_withdraw_center)
    TextView tvWithdraw;
    @Bind(R.id.portrait_center)
    CircleImageView portraitCenter;
    @Bind(R.id.to_login)
    TextView toLogin;
    @Bind(R.id.recharge_center)
    FrameLayout rechargeCenter;
    @Bind(R.id.withdraw_center)
    FrameLayout withdrawCenter;
    @Bind(R.id.account_details)
    CellView accountDetails;
    @Bind(R.id.user_info)
    CellView userInfo;
    @Bind(R.id.change_password)
    CellView changePassword;
    @Bind(R.id.name_center)
    TextView nameCenter;
    @Bind(R.id.cash_center)
    TextView cashCenter;

    @Override
    protected int layoutId() {
        return R.layout.fragment_center;
    }

    @Override
    public void onCreateView2(View view, @Nullable Bundle savedInstanceState) {
        DrawableUtils.setTextViewDrawable(tvRecharge, R.drawable.ic_recharge, Gravity.START);
        DrawableUtils.setTextViewDrawable(tvWithdraw, R.drawable.ic_withdraw, Gravity.START);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (PalmSession.isLogin()) {
            nameCenter.setVisibility(View.VISIBLE);
            nameCenter.setText(PalmSession.getGlobal().getRealName());
            toLogin.setVisibility(View.GONE);
            PalmRetrofit.getInstance().getApi().queryCash(new Request<Void>()).enqueue(new PalmCallBack<UserCash>(getActivity()) {
                @Override
                public void succeed(UserCash userCash) {
                    cashCenter.setVisibility(View.VISIBLE);
                    cashCenter.setText(String.format(Locale.getDefault(), "用户余额\n%.2f", userCash.getCash()));
                }

                @Override
                public void failed(String message) {

                }
            });
        } else {
            nameCenter.setVisibility(View.GONE);
            cashCenter.setVisibility(View.GONE);
            toLogin.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.to_login, R.id.recharge_center, R.id.withdraw_center, R.id.account_details, R.id.user_info, R.id.change_password})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.to_login:
                startActivity(new Intent(getContext(), LoginActivity.class));
                break;
            case R.id.recharge_center:
                break;
            case R.id.withdraw_center:
                //提现
                startActivity(new Intent(getContext(), WithdrawActivity.class));
                break;
            case R.id.account_details:
                break;
            case R.id.user_info:
                startActivity(new Intent(getContext(), UserInfoActivity.class));
                break;
            case R.id.change_password:
                startActivity(new Intent(getContext(), ModifyPwdActivity.class));
                break;
        }
    }


}
